package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.EnterpriseRecommendDO;
import com.triones.ljr.entity.vo.EnterpriseRecommendVO;
import com.triones.ljr.mapper.EnterpriseRecommendMapper;
import com.triones.ljr.service.EnterpriseRecommendService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 推荐企业
 */
@Service
@RequiredArgsConstructor
public class EnterpriseRecommendServiceImpl implements EnterpriseRecommendService {

    private final EnterpriseRecommendMapper enterpriseRecommendMapper;

    @Override
    public TableDataInfo<EnterpriseRecommendVO> pageList(EnterpriseRecommendDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<EnterpriseRecommendDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(EnterpriseRecommendDO::getIsDel, "0")
            .eq(StringUtils.isNotBlank(dto.getStatus()), EnterpriseRecommendDO::getStatus, dto.getStatus())
            .and(ObjectUtil.isNotEmpty(dto.getName()),
                wrapper ->
                    wrapper.like(EnterpriseRecommendDO::getName, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getUscc, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getAddress, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getLegalRepresentative, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getIndustry, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getTel, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getEstablish, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getRegisteredCapital, dto.getName()).or()
                        .like(EnterpriseRecommendDO::getName, dto.getCoverAnArea()).or()
                        .like(EnterpriseRecommendDO::getMainBusiness, dto.getCoverAnArea())
            )
            .orderByAsc(EnterpriseRecommendDO::getSort);
        Page<EnterpriseRecommendVO> result = enterpriseRecommendMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean add(EnterpriseRecommendDO dto) {
        return enterpriseRecommendMapper.insert(dto) > 0;
    }

    @Override
    public boolean edit(EnterpriseRecommendDO dto) {
        return enterpriseRecommendMapper.updateById(dto) > 0;
    }

    @Override
    public EnterpriseRecommendVO getInfo(Long id) {
        return enterpriseRecommendMapper.selectVoById(id);
    }

    @Override
    public boolean remove(Long id) {
        EnterpriseRecommendDO enterpriseRecommendDO = enterpriseRecommendMapper.selectById(id);
        if (enterpriseRecommendDO == null) {
            throw new ServiceException("信息不存在");
        }
        enterpriseRecommendDO.setIsDel("1");
        return enterpriseRecommendMapper.updateById(enterpriseRecommendDO) > 0;
    }

    @Override
    public void saveBatch(List<EnterpriseRecommendDO> list) {
        enterpriseRecommendMapper.insertBatch(list);
    }

    @Override
    public boolean checkNameUnique(EnterpriseRecommendDO dto) {
        boolean exist = enterpriseRecommendMapper.exists(new LambdaQueryWrapper<EnterpriseRecommendDO>()
            .eq(EnterpriseRecommendDO::getName, dto.getName())
            .eq(EnterpriseRecommendDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), EnterpriseRecommendDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public EnterpriseRecommendDO selectByName(String name) {
        return enterpriseRecommendMapper.selectOne(new LambdaQueryWrapper<EnterpriseRecommendDO>()
            .eq(EnterpriseRecommendDO::getName, name).eq(EnterpriseRecommendDO::getIsDel, "0")
            .last("limit 1"));
    }

    @Override
    public List<String> getIndustryCategoryList() {
        List<EnterpriseRecommendDO> recommendDOS = enterpriseRecommendMapper.selectList();
        if (recommendDOS != null && !recommendDOS.isEmpty()) {
            return recommendDOS.stream()
                .map(EnterpriseRecommendDO::getIndustry)
                .distinct()
                .collect(Collectors.toList());

        }
        return null;
    }

    @Override
    public TableDataInfo<EnterpriseRecommendVO> getEnterpriseRecommendPageList(String industryCategory, PageQuery pageQuery) {
        LambdaQueryWrapper<EnterpriseRecommendDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(EnterpriseRecommendDO::getIsDel, "0")
            .eq(EnterpriseRecommendDO::getStatus, "0")
            .eq(StringUtils.isNotBlank(industryCategory), EnterpriseRecommendDO::getIndustry, industryCategory)
            .orderByAsc(EnterpriseRecommendDO::getSort);
        Page<EnterpriseRecommendVO> result = enterpriseRecommendMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
